package com.socket.serializableTest;

import com.socket.socketObjTest.Pet;

import java.io.*;
import java.net.Socket;

/**
 * ThreadSocket--bdqnPractice
 *
 * @author: Jovi
 * @createTime: 2017-09-14 14:12
 **/
public class ThreadSocket implements Runnable{

    Socket socket;

    public ThreadSocket(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            InputStream is = socket.getInputStream();
            ObjectInputStream ois = new ObjectInputStream(is);

            OutputStream os = socket.getOutputStream();
            PrintWriter pw = new PrintWriter(os);
            FileOutputStream fos = new FileOutputStream(new File("pet.txt"),true);

            Pet pet = (Pet)ois.readObject();

            String petStr = pet.toString();

            byte[] temp = petStr.getBytes();
            fos.write(temp, 0, temp.length);
            fos.flush();

            String repay = "congratulation!";
            pw.write(repay);
            pw.flush();

            pw.close();
            fos.close();
            ois.close();
            pw.close();
            os.close();
            is.close();
            socket.close();


        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
}
